<?php
// ----------------------------------------------------------------------
// pn-commerce Shopping Cart Solution for PostNuke
// http://pncommerce.de
// http://www.sourceforge.net/projects/pn-commerce/
//
// It is based on PostKart 0.701QA from
//			Destin LeBlanc [ postkart@zenflesh.com ]
// ----------------------------------------------------------------------
// Source file:  $Source: /cvsroot/pn-commerce/pn-commerce/pnprphotoshareapi.php,v $
//
// Last changes from:  $Author: landseer $
//
// Revision:           $Revision: 1.3 $
//
// Date of revision    $Date: 2005/03/23 12:09:44 $
//
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------

require_once("modules/pncommerce/common.php");

/**
 * This is just to show the name of this api in the scheme edit area
 * This is a necessary function, don't remove it!
 *
 * @return string
 */
function pncommerce_prphotoshareapi_info ($args)
{
    if(!pnModAvailable('photoshare')) {
        return false;
    }
    return _PNC_PHOTOSHAREAPI;
}

/**
 * This function is called during the getItem procedure
 * It sets a field of $args[object] with the name $args['object']['property']['name'] to the value of the stringfield field of the property field
 * This is a necessary function, don't remove it!
 * @param $args[object] array the object that needs to be altered
 * @param $args[property] the propertydata for us
 * @return array
 */
function pncommerce_prphotoshareapi_load ($args)
{
    extract($args);
    unset($args);

    if(!isset($object[$property['name']])) {
        $object['dynamicfields'][] = $property['name'];
    }
    $object[$property['name']] = $property['stringfield'];
    return $object;
}


/**
 * This function returns the stuff from the property array for the given purpose
 * The api can decide which template to use. Even formparts are possible
 * see the processForm for details about this
 * @param $args[purpose] string the porpose for example itemview, cartview or something like that
 * @param $args[purposedata] array special data for this purpose, for example the old and new amount of the item in the cart or something like that
 * @param $args[object] array the object that needs to be altered
 * @param $args[property] the propertydata for us
 * @return array
 */
function pncommerce_prphotoshareapi_getVisualOutput ($args)
{
    switch($args[purpose])
    {
      case "showtemplate":
         $template=$args['purposedata'];
         break;
      case "objectedit":
         $template="objectedit";
         break;
      case "schemeedit":
         $template="schemeedit";
         break;
//  This is a senseless example
      case "cartview":
      case "invoiceview":
      default:
         $template="objectview";
    }
    // prepare some data accordingto the settings
    $imagedata = unserialize($args['property']['stringfield']);
    if(is_array($imagedata) && !empty($imagedata['image'])) {
        $width = "";
        $height = "";
        if(!empty($imagedata['forcewidth'])) {
            $width = 'width="' . $imagedata['forcewidth'] . "'";
        }
        if(!empty($imagedata['forceheight'])) {
            $width = 'height="' . $imagedata['forceheight'] . "'";
        }
        switch($imagedata['showas']) {
            case "1":  // thumbnail only
                $imagedata['imagetag'] = '<img src="' . $imagedata['image'] . '&thumbnail=1" alt="' . $args['property']['name'] . '" "' . $width . '" "' . $height . '">';
                break;
            case "3":  // fullsize image
                $imagedata['imagetag'] = '<img src="' . $imagedata['image'] . '" alt="' . $args['property']['name'] . '" "' . $width . '" "' . $height . '">';
                break;
            case "4":  // thmubnail link with special target
                $imagedata['imagetag'] = '<a href="' . $imagedata['target'] . '" title="' . $args['property']['name'] . '"><img src="' . $imagedata['image'] . '&thumbnail=1" "' . $width . '" "' . $height . '"></a>';
                break;
            case "5":  // full ize imagelink wit special target
                $imagedata['imagetag'] = '<a href="' . $imagedata['target'] . '" title="' . pnVarPrepForDisplay(_PNC_PRPHOTOSHAREGOTO) . " " . $imagedata['target'] . '"><img src="' . $imagedata['image'] . '" "' . $width . '" "' . $height . '"></a>';
                break;
            case "2":
            default:   // thumbnail as link is default
                $imagedata['imagetag'] = '<a href="' . $imagedata['image'] . '" title="' . pnVarPrepForDisplay(_PNC_PRPHOTOSHAREGOTO) . " " . $imagedata['target'] . '"><img src="' . $imagedata['image'] . '&thumbnail=1" "' . $width . '" "' . $height . '"></a>';
        }
    } else {
        $imagedata['imagetag'] = "";
    }

    $smarty =& new pnRender('pncommerce');
    $smarty->caching = false;
    $smarty->assign( 'params', $args );
    $smarty->assign( 'imagedata', $imagedata);

    return $smarty->fetch( 'pncommerce_prphotoshareapi_' . $template . '.html' );
}


/**
 * This function processes a form if necessary (for dropdown fields for sizes etc.)
 * Data should be processed and the modified objectarray has to be returned.
 * all data that should be saved permanently has to be stored in the apropriate propertiesdata field
 * SchemeEdit has it's own process function!
 * -
 * @param $args[purpose] string the porpose for example itemview, cartview or something like that
 * @param $args[purpose data] array special data for this purpose, for example the old and new amount of the item in the cart or something like that
 * @param $args[object] array the object that needs to be altered
 * @param $args[property] the propertydata for us
 * @return array
 */
function pncommerce_prphotoshareapi_processForm ($args)
{
    extract($args);
    unset($args);

//This is a senseless example
    switch($purpose)
    {
        case "objectedit":
            list($imagedata['image'],
                 $imagedata['showas'],
                 $imagedata['forcewidth'],
                 $imagedata['forceheight'],
                 $imagedata['target'])
                          = pnVarCleanFromInput("prphotoshareapi_".$property['propertydata_id']."_".$property['object_id']."_image",
                                                "prphotoshareapi_".$property['propertydata_id']."_".$property['object_id']."_showas",
                                                "prphotoshareapi_".$property['propertydata_id']."_".$property['object_id']."_forcewidth",
                                                "prphotoshareapi_".$property['propertydata_id']."_".$property['object_id']."_forceheight",
                                                "prphotoshareapi_".$property['propertydata_id']."_".$property['object_id']."_target");

            $object['PropertiesData'][$property['propertydata_id']]['stringfield'] = serialize($imagedata);
            $object[$property['name']]=$property['stringfield'];
        default:
      //<sleep>zzZzZzz</sleep>
    }
    return $object;
}

/**
 * This function processes the scheme form and saves the data to the DB
 * @param $args[oldproperty] the old property
 */
function pncommerce_prphotoshareapi_processSchemeForm ($args)
{
    extract($args);
    unset($args);

    if (!pnModAPILoad('pncommerce', 'admin')) {
        return showerrorpage("unable to load pnadminapi", __FILE__, __LINE__);
    }
//We should check for changes here and save only if necessary
//can be improved later :-)
    if(pnVarCleanFromInput($oldproperty['property_id']."_prphotoshareapi_delete") <> '1') {
        $prop=array('property_id'   => $oldproperty['property_id'],
                    'scheme_id'     => $oldproperty['scheme_id'],
                    'type'          => "prphotoshare");
        $prop['name'] = pnVarCleanFromInput($oldproperty['property_id']."_prphotoshareapi_name");

        $config = array();
        $config['defaultimage'] = pnVarCleanFromInput($oldproperty['property_id']."_prphotoshareapi_defaultimage");
        $config['hideifempty'] = (pnVarCleanFromInput($oldproperty['property_id']."_prphotoshareapi_hideifempty")==1) ? 1: 0;
        $prop['config'] = $config;

        $hidden = pnVarCleanFromInput($oldproperty['property_id']."_prphotoshareapi_hidden");
        $prop['hidden'] = ($hidden==1) ? 1 : 0;
        //$prop['sortorder']=pnVarCleanFromInput($args['oldproperty']['property_id']."_prphotoshareapi_sortorder");
        return pnModAPIFunc( 'pncommerce', 'admin', 'propertyStore', $prop);
    } else {
        return pnModAPIFunc( 'pncommerce', 'admin', 'propertyDelete', array('property_id' => $oldproperty['property_id']));
    }
}

/**
 * This function returns a new property with defaultdata
 * @return array
 */
function pncommerce_prphotoshareapi_new ($args)
{
    extract($args);
    unset($args);

    $prop=array('property_id' => -1,
              'scheme_id'     => -1,
              'type'          => "prphotoshare");
    $prop['name'] = pnVarPrepForDisplay(_PNC_PRPHOTOSHARE_DEFAULT);
;
    $prop['config'] = "";//pnVarCleanFromInput($args['property_id']."_prphotoshare_string");
    $prop['hidden'] = 0;
    $prop['sortorder'] = (int)$sortorder;
    return $prop;
}

/**
 * This function returns a new propertydata block with defaultdata
 * @param $args['property'] array property
 * @return array
 */
function pncommerce_prphotoshareapi_newdata ($args)
{
    extract($args);
    unset($args);

    $property['stringfield'] = $property['config']['defaultvalue']; //default value would be good here
    return $property;
}

?>